package com.think.service;

import com.alibaba.fastjson.JSONObject;
import com.think.model.User;
import com.think.util.HttpServletRequestUtil;
import com.think.util.JSONObjectUtil;
import org.springframework.security.web.authentication.WebAuthenticationDetails;

import javax.servlet.http.HttpServletRequest;

public class CustomWebAuthenticationDetails extends WebAuthenticationDetails {

    private final User user;

    public CustomWebAuthenticationDetails(HttpServletRequest request) {
        super(request);
        String userString = HttpServletRequestUtil.readAsChars(request);
        JSONObject jsonObject = JSONObjectUtil.entityToJSON(userString);
        user = JSONObjectUtil.jsonToBean(jsonObject, User.class);
    }

    public User getUser() {
        return user;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(super.toString()).append("; Token: ").append(this.getUser());
        return sb.toString();
    }
}